//
//  ViewController.m
//  04-UIControl
//
//  Created by dllo on 16/7/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UISegmentedControl *segControl;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor blackColor];
#pragma mark - UISwitch
    // Switch 宽和高不影响控件的大小, 只有x和y
    UISwitch *switc = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
    // 开启时的控件内部颜色
    switc.onTintColor = [UIColor yellowColor];
    // 边框及切换到关闭时的颜色 (null_resettable)
    switc.tintColor = [UIColor lightGrayColor];
    // 开关按钮的颜色
    switc.thumbTintColor = [UIColor cyanColor];
    // 打开时的图片, iOS7后失效
    switc.onImage = [UIImage imageNamed:@"4.jpg"];
    // 判断switchControl的开闭状态
    NSLog(@"%@", switc.isOn ? @"YES" : @"NO");
    // 改变switch的初始状态
//    switc.on = YES;
    [switc setOn:YES animated:YES];
    // 添加绑定事件(与button相同)
    [switc addTarget:self action:@selector(switcChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:switc];
    
#pragma mark - UISlider
    // 高度不影响视图的高度, 这个控件用于居中于视图中间
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 200, 200, 100)];
    slider.minimumValue = 0.0f;
    slider.maximumValue = 1.0f;
    // 滑动结束后触发事件
    slider.continuous = NO;
//    // 已移动区域颜色
//    slider.minimumTrackTintColor = [UIColor yellowColor];
//    // 未移动到的区域颜色
//    slider.maximumTrackTintColor = [UIColor blueColor];
//    // 圆球颜色
//    slider.thumbTintColor = [UIColor blackColor];
    // 最小值一侧显示的图片
    [slider setMinimumValueImage:[UIImage imageNamed:@"icon_safe_select"]];
    // 最大值一侧显示的图片
    [slider setMaximumValueImage:[UIImage imageNamed:@"icon_safe_select"]];
    // 设置进度条滑块左边的图片
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    // 设置进度条滑块右边的图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
    // 设置滑块图片
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    
#pragma mark - UISegmentedControl
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"first", @"last"]];
    self.segControl = segmentControl;
    // segment不给frame时 系统会自动计算大小(推荐: 加上frame)
    NSLog(@"%@", NSStringFromCGRect(segmentControl.frame));
    segmentControl.frame = CGRectMake(0, 500, 150, 50);
    // 1 momentary 设置为YES, 点击后没有选中效果
    // 默认为NO
    segmentControl.momentary = YES;
    // 2 Segments个数
    NSUInteger numberOfSeg = segmentControl.numberOfSegments;
    // 3 insertSegmentWithTitle:atIndex:animated
    [segmentControl insertSegmentWithImage:[UIImage imageNamed:@"icon_safe_select"] atIndex:1 animated:YES];
    // 4 setTitle: forSegmentAtIndex: 修改对应下标的标题
    [segmentControl setTitle:@"第一个" forSegmentAtIndex:0];
    // 5 setWidth:forSegmentAtIndex: 改变对应下标分段的宽度
    [segmentControl setWidth:0 forSegmentAtIndex:0];
    // 6 偏移量
//    [segmentControl setContentOffset:CGSizeMake(20, 0) forSegmentAtIndex:0];
    // 7 enabled 是否能被选中
//    [segmentControl setEnabled:NO forSegmentAtIndex:0];
    // 8 selectedSegmentIndex (readwrite)
    segmentControl.selectedSegmentIndex = 1;
    // 9 文字及边框颜色
    segmentControl.tintColor = [UIColor redColor];
    // 10 apportionsSegmentWidthsByContent 默认为NO
    // 如果分段控制器的段宽为0, 并且该属性为YES, 系统会自动计算宽度
    segmentControl.apportionsSegmentWidthsByContent = YES;
    [segmentControl addTarget:self action:@selector(segmentControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentControl];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(250, 300, 50, 50);
    [button setTitle:@"clcik" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
#pragma mark - UIPageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 300, 200, 50)];
    // 分页控制器的页数, 默认为0
    pageControl.numberOfPages = 3;
    // 当前页
    pageControl.currentPage = 2;
    // 只有一页时隐藏
    pageControl.hidesForSinglePage = YES;
    // 获取到点的大小
    CGSize size = [pageControl sizeForNumberOfPages:0];
    NSLog(@"%@", NSStringFromCGSize(size));
    // 未选中page的颜色
    pageControl.pageIndicatorTintColor = [UIColor redColor];
    // 关闭系统事件page的切换, 直到去调用updateCurrentPageDisplay的方法才会显示 ~~~~~~~~
    pageControl.defersCurrentPageDisplay = YES;
    // 当前page的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    [pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl];
}

- (void)pageControlAction:(UIPageControl *)pagecontrol {
    [pagecontrol updateCurrentPageDisplay];
    NSLog(@"%ld", pagecontrol.currentPage);
}

- (void)buttonDidClicked:(UIButton *)sender {
    [_segControl insertSegmentWithTitle:@"second" atIndex:1 animated:YES];
}

// switch触发事件
- (void)switcChanged:(UISwitch *)switc {
    // 根据switch的状态, 决定触发什么事件
    if (switc.on == YES) {

    } else {
        
    }
}

- (void)sliderAction:(UISlider *)slider {
    NSLog(@"%f", slider.value);
}

- (void)segmentControlAction:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
            NSLog(@"1");
            break;
        case 1:
            NSLog(@"12");
            break;
        case 2:
            NSLog(@"123");
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
